// pages/travel//travel/index.js
import {
  list,
  find
} from '../../../api/scenicSpot'
import {
  nlist
} from '../../../api/shop'
const util = require('../../../utils/util')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareDisable: false,
    APP_OSSURL_API: app.APP_OSSURL_API,
    imageList: new Array(4).fill(app.APP_OSSURL_API + "/dinnerBar/lvyou/meizhoudao.jpg"),
    bannerImageUrl: "/dinnerBar/canyin/chushituan/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20201217172030.png",
    starLImageUrl: "/dinnerBar/canyin/canyin-daxingxing.png",
    starLImageUrl: "/dinnerBar/canyin/canyin-daxingxing.png", //灰色星星
    starNImageUrl: "/dinnerBar/canyin/canyin-xingji.png",
    packageStarImageUrl: "/dinnerBar/canyin/canyin-pingfen-red.png",
    cookImageUrl: "/dinnerBar/canyin/chushituan/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20201217171938.png",
    packageImageUrl: "/dinnerBar/canyin/chushituan/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20201217171824.png",
    dishImageUrl: "/dinnerBar/canyin/chushituan/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20201217172059.png",
    fxImageUrl: "/dinnerBar/lvyou/lvyou-fenxiang.png",
    yjtImageUrl: "/dinnerBar/lvyou/lvyou-gengduo.png",
    dwImageUrl: "/dinnerBar/lvyou/lvyou-dizhi.png",
    dhImageUrl: "/dinnerBar/lvyou/lvyou-daohang.png",
    sjtImageUrl: "/dinnerBar/lvyou/lvyou-fangxiangshang.png",
    fjjdImageUrl: "/dinnerBar/redian/screenshot/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20201217142620.png",
    gdrImageUrl: "/dinnerBar/lvyou/lvyou-gengduoren.png",
    xxxImageUrl: "/dinnerBar/common/redian-zanxin.png",
    xxwImageUrl: "/dinnerBar/common/lvyou-weizanxin.png",
    xlImageUrl: "/dinnerBar/WechatIMG192.png",
    wxjtImageUrl: "/dinnerBar/redian/screenshot/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20201217142521.png",
    pingfenImageUrl: "/dinnerBar/ic_list_star_red%402x.png",
    mzdImageUrl: "/dinnerBar/lvyou/meizhoudao.jpg",
    jiantouxiaImageUrl: "/dinnerBar/shouye/shouye-sousuoxiangguanneirong.png",
    jiantoushangImageUrl: "/dinnerBar/lvyou/lvyou-fangxiangshang.png",
    isF: false
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (this.data.shareThumbnail) {
      return {
        imageUrl: this.data.shareThumbnail
      }
    }
  },

  /*
  更多景点
*/
scenicClick(e){
  wx.navigateTo({
    url: '/pages/goods/search/index',
  })
},


  /**
   * 分享
   */
  share: function () {
    // if (!this.ifAuth()) {
    //   return
    // }
    this.debounce("shareDisable", true)
  },

  /* 门票预订折叠 */
  tickets: function (e) {
    if (e.currentTarget.dataset.index != this.data.showIndex) {
      this.setData({
        showIndex: e.currentTarget.dataset.index
      })
    } else {
      this.setData({
        showIndex: 0
      })
    }
  },
  project: function (e) {
    if (e.currentTarget.dataset.index != this.data.showIndex) {
      this.setData({
        showIndex: e.currentTarget.dataset.index
      })
    } else {
      this.setData({
        showIndex: 0
      })
    }
  },

  /* 详情点击折叠展开 */
  fold: function () {
    this.setData({
      isF: !this.data.isF
    })
  },

  favourIt: function () {
    util.scenicFavorites(this.data.scenicSpot.id, this.data.favour ? "del" : "save", this)
  },

  /**
   * 获取到附近的店铺
   */
  toMap: function (e) {
    let {
      lat,
      lng
    } = this.data.scenicSpot;
    wx.openLocation({
      latitude: lat,
      longitude: lng,
      complete: function (e) {
        console.log(e)
      }
    })
  },

  /**
   * 获取到附近的店铺
   */
  getNearbyShops: function () {
    // debugger
    let {
      lat,
      lng
    } = this.data.scenicSpot;
    nlist({
      lat: lat,
      lng: lng,
      pageNum: 1,
      pageSize: 6
    }).then(res => {
      let _shops = res.data.data
      _shops = _shops.map((e, i) => {
        return {
          ...e,
          distance: util.rateConverter(e.distance * 1000),
          logo: util.thumbnail(e.logo, 217, 156)
          // thumbnail: util.thumbnail(e.thumbnail, 211, 140)
        }
      })
      this.setData({
        shops: _shops
      });
    })
  },
  /**
   * 获取附近景点
   */
  getNearbyScenicSpot: function () {
    let {
      lat,
      lng
    } = this.data.scenicSpot;
    list({
      lat: lat,
      lng: lng,
      pageNum: 1,
      pageSize: 6
    }).then(res => {
      let _travles = res.data.data
      _travles = _travles.map((e, i) => {
        return {
          ...e,
          distance: util.rateConverter(e.distance * 1000),
          thumbnail: util.thumbnail(e.thumbnail, 211, 140)
        }
      })
      this.setData({
        travles: _travles
      });
    })
  },

  /**
   * 跳转到店铺页面
   */
  toShop: function (e) {
    let _shopId = e.currentTarget.dataset.shopId
    if (!_shopId)
      return
    wx.navigateTo({
      url: `/pages/shop/shopDetail/index?shopId=${_shopId}`,
    })
  },
  clickOrder: function (e) {
    wx.navigateTo({
      url: '../fillOrder/fillOrder'
    })
  },

  /**
   * 跳转到旅游详情页面
   */
  toTravelInfo: function (e) {
    let _scenicSpotId = e.currentTarget.dataset.scenicSpotId
    if (!_scenicSpotId)
      return
    wx.navigateTo({
      url: `/pages/travel/travel/index?scenicSpotId=${_scenicSpotId}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // debugger
    find(options.scenicSpotId || "14").then(res => {
      let _scenicSpot = res.data.scenicSpot
      let _meta = JSON.parse(_scenicSpot.meta)
      _meta = Object.fromEntries(Object.keys(_meta).map(e => {
        let _list = _meta[e]
        Array.isArray(_list) && _list.map(e => {
          return util.thumbnail(e, 750, 463)
        })
        return [e, _list]
      }))
      // debugger
      this.setData({
        scenicSpot: {
          ..._scenicSpot,
          meta: _meta,
          strScore: util.rateConverter(_scenicSpot.score)
        },
        favour: res.data.isAlaud
      })
      console.log("show me the data", this.data.scenicSpot)
      this.getNearbyShops();
      this.getNearbyScenicSpot();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    //可return 自定义分享图片 
  }
})